package com.shadow.study.java.base.util.json;

import lombok.extern.slf4j.Slf4j;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Slf4j
public final class PatternUtils {

    private PatternUtils() {
    }

    /**
     * 从json中解析出key对应的value(正则 qaq).
     *
     * @param json json
     * @param key  key
     * @return {@link String} value
     * */
    public static String getJSONValue(final String json, final String key) {
        Pattern pattern = Pattern.compile("\"" + key + "\":\"[a-zA-Z0-9\\u4e00-\\u9fa5-]+\"");
        Matcher matcher = pattern.matcher(json);
        if (matcher.find()) {
            String x = matcher.group(0).replace("\"" + key + "\":\"", "");
            return x.substring(0, x.length()-1);
        }
        return null;
    }
}
